home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / dbms_mag / 9103 / listdir.prg < prev    next >
Text File  |  1991-02-04  |  4KB  |  142 lines

  1. *
  2. * ListDir.Prg:  Example of processing a complex multidimensional
  3. *               array with a variable structure.
  4. *
  5. *               Craig Yellick, Alto Microcomputer Inc.
  6. *               7107 Ohms Lane, Minneapolis, MN 55439
  7. *               (612) 835-1080   CompuServe 76247,541
  8. *
  9. *               ListDir.Exe will load the file and subdirectory
  10. *               structure of an entire drive volume and then
  11. *               list the structure to the screen.  Optional
  12. *               parameter allows you to specify the starting
  13. *               subdirectory.  Default is the root directory.
  14. *
  15. *  Examples:    C:\> listdir list entire directory structure
  16. *               C:\> listdir \clip5\    list c:\CLIP5\
  17. *
  18. *               (No error checking is performed on the parameter!)
  19. *
  20. *  Notes:       This program is not so notable for what it DOES
  21. *               but HOW it does it.  There are two functions.
  22. *               LoadDir() loads the specified directory structure
  23. *               and returns a single array that contains the ENTIRE
  24. *               directory structure of any arbitrary drive volume.
  25. *               ListDir() accepts such an array and displays the
  26. *               contents formatted clearly on the screen.  Both
  27. *               functions make use of recursion to cleanly and
  28. *               efficiently perform their tasks.  The code looks
  29. *               amazingly simple -- the mark of elegance!
  30. *
  31. * Compiling:    Compile with the /N option because the source code
  32. *               filename, LISTDIR.PRG, is also the name of a function.
  33. *
  34. * Linking:      Use of a pre-linked library (a "PLL" file) containing
  35. *               at least CLIPPER.LIB and EXTEND.LIB will result in
  36. *               a LISTDIR.EXE of under 10 kb.
  37. *
  38. * Enhancements: This code is BEGGING for you to add file size,
  39. *               date, time and file attributes!
  40. *
  41. *-----------------------------------------------------------------------*
  42. *
  43. *  File Header:  The following are required for compiling the
  44. *                functions found in this source code file.
  45. *
  46.  
  47. ***  IFNIL replaces a long-winded run of three lines of code
  48. *    when testing if a parameter was omitted from a function call
  49. *    and then supplying a default value.
  50. *
  51. *    "IF PARAM = NIL" is the 5.0 way to test for unused parameters,
  52. *    much better than Summer '87's cumbersome IF TYPE("PARAM") = "U"
  53. *    or checking PCOUNT() and keeping track of the parameter order.
  54. *
  55. #command IFNIL <var> := <value>  ;
  56.       => if <var> = nil ; <var> := <value> ; endif
  57.  
  58. ***  This Clipper header file contains #DEFINE commands useful
  59. *    when dealing with the output of the DIRECTORY() function.
  60. *
  61. #include "DIRECTRY.CH"
  62.  
  63. * end file header
  64. *-----------------------------------------------------------------------*
  65.  
  66. function Main(path)
  67. *
  68. *  Entry point from DOS.
  69. *
  70.  
  71. ifnil path := "\"
  72.  
  73. @ 0,0 clear
  74. ? "Loading directory structure"
  75. ListDir(LoadDir(path))
  76.  
  77. return nil
  78.  
  79. * end func Main
  80. *-----------------------------------------------------------------------
  81. *
  82. function ListDir(dir_, level)
  83. *
  84. *  Display outer level of a subdirectory and file list
  85. *  in the arrray passed as a parameter.  Optional level
  86. *  number indicates how deep in to consider the "outer" level.
  87. *
  88. *  NOTE: ListDir() calls itself recursively.
  89. *
  90.  
  91. local i
  92. ifnil level := 0
  93. for i= 1 to len(dir_)
  94.   ? space(level *3)
  95.   if valtype(dir_[i]) = "A"
  96.     ?? dir_[i, 1]
  97.     ?? replicate("-", 15 -len(dir_[i, 1]))
  98.     ListDir(dir_[i, 2], level +1)
  99.   else
  100.     ?? dir_[i]
  101.   endif
  102. next i
  103. return nil
  104.  
  105. * end func ListDir
  106. *-----------------------------------------------------------------------
  107. *
  108. function LoadDir(path)
  109. *
  110. *  Load DOS files and subdirectories into single multidemensional array.
  111. *  Optional parameter specifies starting subdirectory.
  112. *  Default is root directory.
  113. *  Returns array containing the structure.
  114. *
  115. *  NOTE:  LoadDir() calls itself recursively.
  116. *
  117.  
  118. local i, name, d_, r_ := {}
  119.  
  120. ifnil path := "\"
  121. d_ := directory(path +"*.*", "D")
  122.  
  123. for i = 1 to len(d_)
  124.   name := d_[i, F_NAME]
  125.   if d_[i, F_ATT] = "D"
  126.     if .not. (name $ "..")
  127.       ?? "."
  128.       aadd(r_, {name, LoadDir(path +name +"\")})
  129.     endif
  130.   else
  131.     aadd(r_, name)
  132.   endif
  133. next i
  134.  
  135. return r_
  136.  
  137. * end func LoadDir
  138. *-----------------------------------------------------------------------*
  139. * eof ListDir.Prg
  140. *______________________________________________________________
  141.  
  142.